package com.zl.smartmall.library.listener;

import org.apache.http.Header;
import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;
import android.util.JsonReader;

import com.zl.smartmall.library.account.LoginPlatform;
import com.zl.smartmall.library.account.po.CookieInfo;
import com.zl.smartmall.library.account.po.UserInfo;
import com.zl.smartmall.library.account.vo.UserInfoDao;
import com.zl.smartmall.library.util.PreferenceModel;

public abstract class PhoneRegisterListener extends BaseListener {

	@Override
	public void onNoMoreData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onOffline() {

	}
	
	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		try {
			int ret = response.getInt("ret");
			switch (ret) {
			case 0:
				JSONObject result = response.getJSONObject("result");
				parseThirdLoginResponse(headers, result);
				break;
			case 1:
				onResponseFailed();
				break;
			case 3:
				onSmsCodeError();
				break;

			default:
				onResponseFailed();
				break;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			onResponseFailed();
		}
		
	}
	
	@Override
	public void onFailure(int statusCode, Header[] headers,
			String responseString, Throwable throwable) {
		super.onFailure(statusCode, headers, responseString, throwable);
		onResponseFailed();
	}

	public abstract void onResponseSuccess(CookieInfo cookieInfo,UserInfo userinfo);
	
	public abstract void onSmsCodeError();
	
	private void parseThirdLoginResponse(Header[] headers,
			JSONObject response) throws JSONException {
//		String cid = PreferenceModel.getString(Define.GT_CID_NAME, "");
//		Global.get().cleanResidualData();
//
		UserInfo userInfo = UserInfo.parse(response);//UserInfo.parse(jsonObject)
//
//		JSONObject obj = response.getJSONObject("result");
//		String cookieValue = obj.getString("token");
//		Global.get().saveCookie(headers,userinfo.getUid(),userinfo.getLoginType());
//		Global.get().resetCookie(cookieValue);
//		onResponseSuccess(userinfo);
//		
//		Global.get().registerPush(cid);
//		Global.get().addSystemHelper(); // 添加系统小助手
//
//		PreferenceModel.putInt(Define.UID, userinfo.getUid());
//		PreferenceModel.putInt(Define.USER_TYPE,userinfo.getLoginType());
		
		if(userInfo != null) {
			userInfo.setLoginPlatform(LoginPlatform.PHONE.getValue());
			CookieInfo cookieInfo = CookieInfo.parse(headers, userInfo.getUid(), userInfo.getLoginType());
			String cookieName = "Set-Cookie";
			String cookieValue = cookieInfo.getCookieValue();
			if (!TextUtils.isEmpty(cookieValue)) {
				CookieStore mCookieStore = new BasicCookieStore();
				BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieValue);
				cookie.setVersion(0);
				cookie.setDomain("");
				cookie.setPath("/");
				mCookieStore.addCookie(cookie);
			}
			onResponseSuccess(cookieInfo,userInfo);
		}else {
			onResponseFailed();
		}
	}
	
}
